using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

namespace ProductExpirationAssistant
{
    /// <summary>下架日期查询记录</summary>
    public class OffShelfDateQuery
    {
        /// <summary>最小生产日期</summary>
        private readonly DateTime MIN_PRODUCTION_DATE = new DateTime(1, 1, 2);

        /// <summary>保质期</summary>
        public QualityGuaranteePeriod qualityGuaranteePeriod { get; set; }
        /// <summary>提前下架天数</summary>
        public int daysInAdvance { get; set; }

        private DateTime m_ProductionDate;
        public DateTime productionDate
        {
            get => m_ProductionDate < MIN_PRODUCTION_DATE ? MIN_PRODUCTION_DATE : m_ProductionDate;
            set
            {
                if (value >= MIN_PRODUCTION_DATE) m_ProductionDate = value;
            }
        }

        public OffShelfDateQuery()
        {
            qualityGuaranteePeriod = new QualityGuaranteePeriod();
        }

        /// <summary>计算下架日期</summary>
        public DateTime CalOffShelfDate()
        {
            DateTime date = new DateTime();

            switch (qualityGuaranteePeriod.unit)
            {
                case DateUnit.Day:
                    {
                        date = productionDate.AddDays(qualityGuaranteePeriod.num);
                        break;
                    }
                case DateUnit.Month:
                    {
                        date = productionDate.AddMonths(qualityGuaranteePeriod.num);
                        break;
                    }
                case DateUnit.Year:
                    {
                        date = productionDate.AddYears(qualityGuaranteePeriod.num);
                        break;
                    }
            }

            return date.AddDays(-1 - daysInAdvance);
        }

        /// <summary>计算过期日期</summary>
        public DateTime CalExpiredDate()
        {
            return CalOffShelfDate().AddDays(daysInAdvance);
        }

        public bool Compare(OffShelfDateQuery query)
        {
            // 比较顺序：生产日期>保质期日期所用的单位>保质期数>提前下架天数
            if (productionDate == query.productionDate)
            {
                if (qualityGuaranteePeriod.unit == query.qualityGuaranteePeriod.unit)
                {
                    if (qualityGuaranteePeriod.num == query.qualityGuaranteePeriod.num)
                    {
                        return daysInAdvance > query.daysInAdvance;
                    }
                    else
                    {
                        return qualityGuaranteePeriod.num > query.qualityGuaranteePeriod.num;
                    }
                }
                else
                {
                    return qualityGuaranteePeriod.unit > query.qualityGuaranteePeriod.unit;
                }
            }
            else
            {
                return productionDate > query.productionDate;
            }
        }
    }
}